package com.fh.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.admin.methods.response.NewAccountIdentifier;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.Request;

import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.core.methods.response.EthTransaction;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.geth.Geth;
import org.web3j.protocol.http.HttpService;
import org.web3j.protocol.parity.Parity;
import org.web3j.utils.Convert;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import net.sf.json.JSONArray;


public class sss {
	private static String gethClientUrl = "http://192.168.1.3:8545";

    //得到当前块高度
    public BigInteger getCurrentBlockNumber() {
        Web3j web3j = initWeb3j();
        Request<?, EthBlockNumber> request = web3j.ethBlockNumber();
        try {

            return request.send().getBlockNumber();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BigInteger.valueOf(0L);
    }
    
    
    //得到当前块高度
    public void getsssw() {
//    	String url = "http://172.16.0.1:8545/";
		Web3j web3j = Web3j.build(new HttpService(gethClientUrl)); // defaults to http://localhost:8545/

		try {
			Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();
			String clientVersion = web3ClientVersion.getWeb3ClientVersion();
			System.out.println(clientVersion);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    
    //查询钱包额度
    public void getsss(String usrid) {
        Web3j web3j = initWeb3j();
//        String address = "0x41F1dcbC0794BAD5e94c6881E7c04e4F98908a87";//等待查询余额的地址
        //第二个参数：区块的参数，建议选最新区块
        EthGetBalance balance;
		try {
			balance = web3j.ethGetBalance(usrid, DefaultBlockParameter.valueOf("latest")).send();
			 String blanceETH = Convert.fromWei(balance.getBalance().toString(), Convert.Unit.ETHER).toPlainString().concat(" ether");
			 System.out.println(blanceETH);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //格式转化 wei-ether
       
     
    }

    
    
    //查询钱包额度
    public String getsssw(String usrid) {
    	Request request =null;
		try {
			 Web3j web3j = initWeb3j();
		      request = web3j.ethGetTransactionByHash(usrid);
		      EthTransaction s= (EthTransaction) request.send(); 
		      System.out.println(s.getResult());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return request.toString(); 
           
    }
    //新建用户
    public String newAccount(String password) {
        Admin admin = initAdmin();
        Request<?, NewAccountIdentifier> request = admin.personalNewAccount(password);
        NewAccountIdentifier result = null;
        try {
            result = request.send();
            return result.getAccountId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //全部用户
    public String accounts() {
        Parity parity = initParity();
        List<String> ids = new ArrayList<>();
        try {
            ids = parity.personalListAccounts().send().getAccountIds();
            System.out.println("用户数量 : " + ids.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
       
        return JSONArray.fromObject(ids).toString();
    }

    /**
     * 初始化web3j普通api调⽤用
     *
     * @return web3j
     */
    private Parity initParity() {
        return Parity.build(getService());
    }


    /**
     * 初始化web3j普通api调⽤用
     *
     * @return web3j
     */
    private Web3j initWeb3j() {
        return Web3j.build(getService());
    }


    /**
     * 初始化personal级别的操作对象
     *
     * @return Geth
     */
    private Geth initGeth() {
        return Geth.build(getService());
    }

    /**
     * 通过http连接到geth节点
     *
     * @return
     */
    private HttpService getService() {
        return new HttpService(gethClientUrl);
    }


    /**
     * 初始化admin级别操作的对象
     *
     * @return Admin
     */
    private Admin initAdmin() {
        return Admin.build(getService());
    }
    
    public List<String> getAccountlist() {
    	Web3j web3j = Web3j.build(new HttpService(gethClientUrl)); // defaults to http://localhost:8545/
		try {
			return web3j.ethAccounts().send().getAccounts();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
    

    public static void main(String[] args) {
//     new sss().getsss("0x7d5098baa6da28caceb8d576ecdf3ee8090702b9");
//      	String accounts = new sss().newAccount("www");
//      	 new sss().getsssw();
//    	 new sss().getsssw();	
      	
//      	System.out.println(accounts);
//      	String accounts = new sss().accounts();
//      	 List s = new sss().getAccountlist();
//      	 new sss().getsssw();
//      	 for(int i=0;i<s.size();i++){
//      		 System.out.println(s.get(i));
//      	 }
    	try {
    		Web3j web3j = Web3j.build(new HttpService(gethClientUrl));
        	String address = "0x23CDfDbB56b634b5dEb0Aa7A1aDcD2b7011d408b";

        	EthGetBalance ethGetBalance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();

        	if(ethGetBalance!=null){
        	// 打印账户余额
        	System.out.println(ethGetBalance.getBalance());
        	// 将单位转为以太，方便查看
        	System.out.println(Convert.fromWei(ethGetBalance.getBalance().toString(), Convert.Unit.ETHER));
        	}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
    	

    			
//        System.out.println(accounts);
    }

}
